package com.rytong.tools.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.RadioButton;

import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.LPLayout.MyLPLayout;
import com.rytong.tools.utils.Utils;

/**
 * @author zhou.yang
 * @Description 开关控件，需要以下几个xml资源文件
 *  switchleft_bg.xml,
 *  switchoff_bg.xml,
 *  switchon_bg.xml,
 *  switchright_bg.xml
 */
@SuppressWarnings("deprecation")
public class LPSwitch extends Component {

    /**
     * 此switch在此组的位置,从0开始
     */
    int groupNum_;

    /**
     * 此组switch的个数
     */
    int groupCount_;

    /**
     * 存放本组switch元素
     */
    List<Component> switchList_;

    // 未选中时字体颜色
    final int SWITCH_OFF = 0xFF000000;
    // 选中时字体颜色
    final int SWITCH_ON = 0xFFFFFFFF;

    private int switchleft_bg_;

    private int switchright_bg_;

    private int switchon_bg_;

    private int switchoff_bg_;

    public class MyLPSwitch extends RadioButton implements CompositedComponent {

        public MyLPSwitch(Context context) {
            super(context);
        }

        @Override
        public Component composited() {
            return LPSwitch.this;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int touchEvent = event.getAction();
            boolean isAction = false;
            switch (touchEvent) {
            case MotionEvent.ACTION_DOWN:
            	// 按下tr时候变色。
            	trDownColor(tr_, llvSV_);
                isAction = true;
                break;
            case MotionEvent.ACTION_UP:
            	// 抬起tr恢复颜色。
            	trUpColor(tr_, llvSV_);
                // 设置选中项
                setCheckedManually(true);
                // 设置segment的lua点击事件
                isAction = onClick((String) composited().property_.get("onclick"));
                break;
            default:
                isAction = super.onTouchEvent(event);
            }
            return isAction;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = getPaint();
            if (this.isChecked()) {
                if (groupNum_ == 0) {
                    setBackgroundResource(switchon_bg_);
                } else {
                    setBackgroundResource(switchoff_bg_);
                }
                paint.setStrokeWidth(5);
                if (groupNum_ == 0) {
                    paint.setColor(SWITCH_ON);
                    canvas.drawLine(getWidth()/2, getHeight()/4, getWidth()/2, getHeight()*3/4, paint);
                } else {
                    paint.setColor(SWITCH_OFF);
                    paint.setStyle(Style.STROKE);
                    canvas.drawCircle(getWidth()/2, getHeight()/2, getHeight()/4, paint);
                }

            }else{
                if (groupNum_ == 0) {
                    setBackgroundResource(switchleft_bg_);
                } else {
                    setBackgroundResource(switchright_bg_);
                }
            }
        }

    }

    @Override
    public void initRealView(Activity arg0, String arg1) {
        super.initRealView(arg0, arg1);
        realView_ = new MyLPSwitch(arg0);
    }

    @Override
    public void mouldH() throws Exception {
        super.mouldH();
        LayoutParams layoutParams = (LayoutParams) getLayoutParams();
        width_ = layoutParams.width;
        height_ = layoutParams.height;
        // 设置value值
        String name = (String) property_.get("name");
        Component c = getParent();
        int childCount = c.childrenList_.size();
        Component child = null;
        String childName = null;
        int segmentCount = 0;
        switchList_ = new ArrayList<Component>();
        // 获取界面和此semgent在同一组的segment的个数
        for (int i = 0; i < childCount; i++) {
            child = (Component) c.childrenList_.get(i);
            childName = (String) child.property_.get("name");
            if (childName != null && childName.equals(name)) {
                switchList_.add(child);
                // 计算segment元素在当前组的位置
                if(child == this){
                    groupNum_ = segmentCount;
                }
                // 计算当前segment组的个数
                segmentCount++;
            }
        }
        groupCount_ = segmentCount;
        // 设置segment的宽、高、位置
        width_ = width_ / segmentCount;
        // 设置此segment的layoutparams
        layoutParams.width = width_;
        layoutParams.x = groupNum_ * width_;
        setLayoutParams(layoutParams);

        // 获取背景图id
        getSwitchBgId();

        // 设置选中项
        String checked = (String) property_.get("checked");
        if (checked != null) {
            setCheckedManually(true);
        } else {
            setCheckedManually(false);
        }
    }

    public void setCheckedManually(final boolean isChecked) {
        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (isChecked) {
                    Component com = null;
                    MyLPSwitch myLPSwitch = null;
                    for (int i = 0; i < switchList_.size(); i++) {
                        com = (Component) switchList_.get(i);
                        myLPSwitch = (MyLPSwitch) com.realView_;
                        myLPSwitch.setChecked(false);
                    }
                }
                MyLPSwitch lpSwitch = (MyLPSwitch) realView_;
                lpSwitch.setChecked(isChecked);
                Component parent = getParent();
                parent.invalidate();
            }
        });

    }

    private void getSwitchBgId() {
        switchleft_bg_ = Utils.getResourcesId(getActivity(), "switchleft_bg", "drawable");
        switchright_bg_ = Utils.getResourcesId(getActivity(), "switchright_bg", "drawable");
        switchon_bg_ = Utils.getResourcesId(getActivity(), "switchon_bg", "drawable");
        switchoff_bg_ = Utils.getResourcesId(getActivity(), "switchoff_bg", "drawable");
    }
    
    @Override
    public void releaseResource(Component component) {
    	super.releaseResource(component);
    	if(switchList_ != null){
    		switchList_.clear();
    		switchList_ = null;
    	}
    }

}
